//
//  HJCompanyController.m
//  Keping
//
//  Created by a on 2017/8/30.
//  Copyright © 2017年 柯平. All rights reserved.
//

#import "HJCompanyController.h"

#import "HJVisitFriendMessageCell.h"

@interface HJCompanyController ()<UITableViewDelegate,UITableViewDataSource>
/**
 tableView
 */
@property(nonatomic,weak)UITableView*tableView;
/**
 数据源
 */
@property(nonatomic,strong)NSMutableArray*dataAry;
@property (nonatomic, strong) NSMutableDictionary *companyDict;

@end

@implementation HJCompanyController
-(NSMutableArray *)dataAry
{
    if (!_dataAry) {
        _dataAry=[NSMutableArray arrayWithObjects:@[@"* Company Name"],@[@"* Contact Number"],@[@"Email Address"],@[@"Remarks"], nil];
    }
    return _dataAry;
}

- (NSMutableDictionary *)companyDict {
    if (!_companyDict) {
        _companyDict = [[NSMutableDictionary alloc] init];
    }
    return _companyDict;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUI];
}
-(void)setUI
{
    self.navigationItem.title=@"Company";
    UITableView*tableView=[[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    tableView.delegate=self;
    tableView.dataSource=self;
    tableView.backgroundColor=[UIColor colorWithHexString:@"#f2f1f6"];
    [self.view addSubview:tableView];
    self.tableView=tableView;
    
    
    //添加table尾部
    UIView*footerView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, App_Frame_Width, K_FactorH(100))];
    tableView.tableFooterView=footerView;
    footerView.backgroundColor=[UIColor clearColor];
    CGFloat margin =K_FactorW(20);
    UIButton*addBtn=[[UIButton alloc]initWithFrame:CGRectMake(margin, K_FactorH(30), footerView.width-2*margin, footerView.height-K_FactorH(30)-margin)];
    [footerView addSubview:addBtn];
    addBtn.layer.cornerRadius=addBtn.height/2;
    addBtn.layer.masksToBounds=YES;
    [addBtn setBackgroundColor:[UIColor colorWithHexString:@"#6853fa"] forState:UIControlStateNormal];
    [addBtn setTitle:@"Proceed" forState:UIControlStateNormal];
    addBtn.titleLabel.font=[UIFont boldSystemFontOfSize:20];
    [addBtn addTarget:self action:@selector(clickProceed) forControlEvents:UIControlEventTouchUpInside];
    
}
-(void)clickProceed
{
    if (self.companMessageBlock) {
        self.companMessageBlock([self.companyDict copy]);
    }
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark --UITableViewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataAry.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.dataAry[section] count];
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger section = indexPath.section;
    NSInteger row     = indexPath.row;
    WEAKSELF
    if (section == self.dataAry.count - 1) {
        HJVisitFriendMessageCell*cell=[HJVisitFriendMessageCell cellWithTableView:tableView];
        cell.title=self.dataAry[indexPath.section][indexPath.row];
        cell.visitFriend = ^(id cell, NSString *contet) {
            [weakSelf.companyDict setObject:contet forKey:@"message"];
        };
        return cell;
    }else
    {
        HJVisitFriendViewCell*cell = [HJVisitFriendViewCell cellWithTableView:tableView];
        cell.title = self.dataAry[section][row];
        cell.visitFriend = ^(id cell, NSString *contet) {
            NSIndexPath *indexPath = [weakSelf.tableView indexPathForCell:cell];
            NSInteger sectionTmp = indexPath.section;
            switch (sectionTmp) {
                case 0:
                    [weakSelf.companyDict setObject:contet forKey:@"companyName"];
                    break;
                case 1:
                    [weakSelf.companyDict setObject:contet forKey:@"phone"];
                    break;
                case 2:
                    [weakSelf.companyDict setObject:contet forKey:@"email"];
                    break;
                case 3:
                    [weakSelf.companyDict setObject:contet forKey:@"message"];
                    break;
                default:
                    break;
            }
        };
        cell.isEdit = YES;
        return cell;
    }
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==self.dataAry.count-1) {
        return K_FactorH(240);
    }
    return K_FactorH(60);
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return K_FactorH(10);
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.1f;
}

@end
